/*
 * gc_test_manyObjects2.c
 *
 *  Created on: Dec 9, 2011
 *      Author: Rui Zhuang
 */

#include "gc.h"

struct A {
	struct A* a;
};

int main(int argc, char** argv){

	g_debug = TRUE;
	struct A* a;
	gc_add_root_node(&a);

	printf("Sizeof struct A is %d\n",sizeof(struct A));
	int i = 1;
	printf("Maximum memory size avaiable = %d\n", g_nMaxHeapSize);
	printf("Before objects allocation: Current heap size = %d\n\n", g_nCurrentHeapSize);

	a = (struct A*)gc_malloc(sizeof(struct A));
	printf("%d object allocated\n\n", i);
	i += 1;
	struct A* temp = a;
	gc_add_root_node(&temp);
	while(i <= 3){
		temp->a = (struct A*)gc_malloc(sizeof(struct A));
		temp = temp->a;
		printf("%d objects allocated\n\n", i);
		i=i+1;
	}
	printf("After %d objects allocated: Current heap size = %d\n\n", i-1, g_nCurrentHeapSize);
	printf("Now we have the root nodes as : \n");
	gc_output_root_nodes();
	printf("Now we have the heap nodes as : \n");
	gc_output_heap_nodes();

	printf("Then we free all the root nodes and do gc_recycle....\n");
	gc_free_root_node(&a);
	printf("root node (ptr = %d) freed...\n", (int)a);
	gc_free_root_node(&temp);
	printf("root node (ptr = %d) freed...\n", (int)temp);
	gc_recycle();
	printf("After recycle: current heap size = %d\n\n", g_nCurrentHeapSize);
	printf("Successfully run gc_test_nodesInfoOutput!\n");
	return 0;
}
